<div class="tutorial-bundle" data-metadata-json='{"difficulty": "beginner"}' data-tip-sha1="9919ab8701e5ab044c809d513ed90a4f7b55801b"><div class="front-matter" data-complete-code-text='# Move the bird using the arrow keys and try to catch the star!

import pytch
import random


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    Sounds = ["honk.wav"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)

    @pytch.when_green_flag_clicked
    def check_catch(self):
        pytch.wait_seconds(0.1)
        while True:
            if self.touching(Star):
                self.start_sound("honk")
                self.say_for_seconds("Got you!", 2)


class Star(pytch.Sprite):
    Costumes = ["star.png"]

    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
        while True:
            self.show()
            destination_x = random.randint(-240, 240)
            destination_y = random.randint(-180, 180)
            self.glide_to_xy(destination_x, destination_y, 2)

    @pytch.when_green_flag_clicked
    def check_caught(self):
        while True:
            if self.touching(Bird):
                self.hide()
' data-initial-code-text=""><h1>Chase game</h1><p>In this tutorial we will make a game where you control a bird that
chases a star around the stage.</p><p>In this project you will learn about making multiple Sprites, making
them move, and detecting when they come close together.</p></div><div class="chapter-content"><h2>Setting up Pytch</h2><p>As usual we have to make sure the Pytch add-ons are available. This
means writing an "import" line at the top of our program.</p><div class="patch-container" data-code-as-of-commit="import pytch
" data-slug="import-pytch"><div class="patch"><table><tbody class="diff-add" data-added-text="import pytch
"><tr><td class="linenum"></td><td class="linenum"><pre>1</pre></td><td><pre>import pytch</pre></td></tr></tbody></table></div></div></div><div class="chapter-content"><h2>Setting up the stage</h2><p>Let's start by setting up the stage for the game. There is a nice
backdrop of some clouds so we define a <em>Stage</em> that will look good
when the bird is flying around.</p><p>Making a Stage in Pytch is rather like making a Sprite, we have to
create a new "class" that says it is a Stage. Then we can define
<em>backdrops</em> as part of that stage.</p><p>You can only have one class in your project for the Stage (there's
only one stage, no matter how many Sprites there are), but it can have
different backdrops if you need that. This project will only have one,
so Pytch will automatically select it when the project starts.</p><div class="patch-container" data-code-as-of-commit='import pytch


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]
' data-slug="add-Stage-with-backdrop"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>1</pre></td><td class="linenum"><pre>1</pre></td><td><pre>import pytch</pre></td></tr></tbody><tbody class="diff-add" data-added-text='

class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]
'><tr><td class="linenum"></td><td class="linenum"><pre>2</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>3</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>4</pre></td><td><pre>class Sky(pytch.Stage):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>5</pre></td><td><pre>    Backdrops = ["clouds.jpg"]</pre></td></tr></tbody></table></div></div><p>You can try the project now — click the <em>green flag</em> to run your
program — and you should see the clouds as a nice backdrop.</p></div><div class="chapter-content"><h2>Making the Bird character</h2><p>Next we will make the bird character that the player will control. The
first step is to make a new Sprite and give it a costume. The costume
we are using is already in the project assets so we just need to make
sure Pytch knows which costume is for this Sprite.</p><div class="patch-container" data-code-as-of-commit='import pytch


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
' data-slug="add-Bird-with-costumes"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>3</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>4</pre></td><td><pre>class Sky(pytch.Stage):</pre></td></tr><tr><td class="linenum"><pre>5</pre></td><td class="linenum"><pre>5</pre></td><td><pre>    Backdrops = ["clouds.jpg"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text='

class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
'><tr><td class="linenum"></td><td class="linenum"><pre>6</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>7</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>8</pre></td><td><pre>class Bird(pytch.Sprite):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>9</pre></td><td><pre>    Costumes = ["bird.png"]</pre></td></tr></tbody></table></div></div><h3>Moving the bird to its starting position</h3><p>The game will start when the player presses the green flag. As soon as
that happens I want the bird to move right to the middle of the
stage. So I <em>def</em>ine a new bit of code that will run when when the
green flag is clicked. I'm going to name this "start", but the name
isn't really important, it's the hat block that controls when this
will be run.</p><div class="patch-container" data-code-as-of-commit='import pytch


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
' data-slug="initialise-bird-position"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>7</pre></td><td class="linenum"><pre>7</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>8</pre></td><td class="linenum"><pre>8</pre></td><td><pre>class Bird(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>9</pre></td><td><pre>    Costumes = ["bird.png"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text="
    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
"><tr><td class="linenum"></td><td class="linenum"><pre>10</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>11</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>12</pre></td><td><pre>    def start(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>13</pre></td><td><pre>        self.go_to_xy(0, 0)</pre></td></tr></tbody></table></div></div><p>If you try this code now — click the green flag to run your program —
you will see that the bird costume is actually rather too big for the
stage. I can fix this by telling the sprite to pick a smaller size
before it appears, using the <code>set_size</code> command. The number given to
<code>set_size</code> controls the size, from 1 (full size) to 0 (so small you
can't see it).</p><div class="patch-container" data-code-as-of-commit='import pytch


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)
' data-slug="set-bird-size"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>11</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>12</pre></td><td><pre>    def start(self):</pre></td></tr><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>13</pre></td><td><pre>        self.go_to_xy(0, 0)</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        self.set_size(0.3)
"><tr><td class="linenum"></td><td class="linenum"><pre>14</pre></td><td><pre>        self.set_size(0.3)</pre></td></tr></tbody></table></div></div><h3>Making the bird move</h3><p>The next job is to let the bird move around the stage so that it can
chase something. I will let the player use the arrow keys to move it
around.</p><p>Before I do that I am going to set up a variable that I will use to
control the speed the bird moves at. I don't <em>have</em> to set my project
up this way but it will make it easier to change how fast the bird
moves if I want to.</p><p>I create a variable in the Bird by adding this line after the costumes:</p><div class="patch-container" data-code-as-of-commit='import pytch


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)
' data-slug="add-bird-speed-variable"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>7</pre></td><td class="linenum"><pre>7</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>8</pre></td><td class="linenum"><pre>8</pre></td><td><pre>class Bird(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>9</pre></td><td><pre>    Costumes = ["bird.png"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text="    speed = 3
"><tr><td class="linenum"></td><td class="linenum"><pre>10</pre></td><td><pre>    speed = 3</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>10</pre></td><td class="linenum"><pre>11</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>12</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>13</pre></td><td><pre>    def start(self):</pre></td></tr></tbody></table></div></div><p>Now I will make some code to move the bird to the right. Once this is
working nicely we can copy it to move in the other directions. I want
this code to run whenever the player presses the right-arrow key, so I
make a script (I have to give it a name, Python needs that), and I
mark it with some code that lets Pytch know this script gets run when
the right-arrow key is pressed.</p><p>I can make the Bird sprite move using the <code>change_x</code> function.  Notice
how I'm using the <code>speed</code> variable that's defined as part of the Bird
to say how far the bird will move.</p><div class="patch-container" data-code-as-of-commit='import pytch


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)
' data-slug="bird-move-right"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>13</pre></td><td><pre>    def start(self):</pre></td></tr><tr><td class="linenum"><pre>14</pre></td><td class="linenum"><pre>14</pre></td><td><pre>        self.go_to_xy(0, 0)</pre></td></tr><tr><td class="linenum"><pre>15</pre></td><td class="linenum"><pre>15</pre></td><td><pre>        self.set_size(0.3)</pre></td></tr></tbody><tbody class="diff-add" data-added-text='
    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)
'><tr><td class="linenum"></td><td class="linenum"><pre>16</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>17</pre></td><td><pre>    @pytch.when_key_pressed("ArrowRight")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>18</pre></td><td><pre>    def move_right(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>19</pre></td><td><pre>        self.change_x(self.speed)</pre></td></tr></tbody></table></div></div><p>If you try the project now then you will be able to make the bird move
in one direction by pressing the right arrow key. Excellent, we are
well on the way!</p><p>Now we should add three move methods for the other three
directions. We can make the bird move to the left by changing the <code>x</code>
value in the negative direction (just put a minus sign in front of the
variable). To move up and down we change the <code>y</code> value of the Sprite.</p><p>One thing to watch out for is that each method needs to have a
different name, or the later ones replace the earlier ones in the
Sprite (which would mean there is only one of them left, and some
directions wouldn't work!).</p><div class="patch-container" data-code-as-of-commit='import pytch


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)
' data-slug="move-bird-three-other-directions"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>17</pre></td><td class="linenum"><pre>17</pre></td><td><pre>    @pytch.when_key_pressed("ArrowRight")</pre></td></tr><tr><td class="linenum"><pre>18</pre></td><td class="linenum"><pre>18</pre></td><td><pre>    def move_right(self):</pre></td></tr><tr><td class="linenum"><pre>19</pre></td><td class="linenum"><pre>19</pre></td><td><pre>        self.change_x(self.speed)</pre></td></tr></tbody><tbody class="diff-add" data-added-text='
    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)
'><tr><td class="linenum"></td><td class="linenum"><pre>20</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>21</pre></td><td><pre>    @pytch.when_key_pressed("ArrowLeft")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>22</pre></td><td><pre>    def move_left(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>23</pre></td><td><pre>        self.change_x(-self.speed)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>24</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>25</pre></td><td><pre>    @pytch.when_key_pressed("ArrowUp")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>26</pre></td><td><pre>    def move_up(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>27</pre></td><td><pre>        self.change_y(self.speed)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>28</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>29</pre></td><td><pre>    @pytch.when_key_pressed("ArrowDown")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>30</pre></td><td><pre>    def move_down(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>31</pre></td><td><pre>        self.change_y(-self.speed)</pre></td></tr></tbody></table></div></div><p>If you try the project out now then you should be able to move the
bird in all four directions.</p><p>If you think the bird is moving a little slowly then you could try
changing the number we store in the <code>speed</code> variable. Because each of
the moving methods uses the number they will all change <code>x</code> or <code>y</code> by
the amount stored in the variable. Remember, you have to build and run
the project after making any changes.</p></div><div class="chapter-content"><h2>Giving the Bird something to chase</h2><p>Now that the bird can move around the screen let's give it something
to chase. Make a new Sprite and set its costume to the "star"
image.</p><div class="patch-container" data-code-as-of-commit='import pytch


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)


class Star(pytch.Sprite):
    Costumes = ["star.png"]
' data-slug="create-Star-with-costumes"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>29</pre></td><td class="linenum"><pre>29</pre></td><td><pre>    @pytch.when_key_pressed("ArrowDown")</pre></td></tr><tr><td class="linenum"><pre>30</pre></td><td class="linenum"><pre>30</pre></td><td><pre>    def move_down(self):</pre></td></tr><tr><td class="linenum"><pre>31</pre></td><td class="linenum"><pre>31</pre></td><td><pre>        self.change_y(-self.speed)</pre></td></tr></tbody><tbody class="diff-add" data-added-text='

class Star(pytch.Sprite):
    Costumes = ["star.png"]
'><tr><td class="linenum"></td><td class="linenum"><pre>32</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>33</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>34</pre></td><td><pre>class Star(pytch.Sprite):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>35</pre></td><td><pre>    Costumes = ["star.png"]</pre></td></tr></tbody></table></div></div><h3>Moving the Star randomly</h3><p>We want the star to move about, but not under the player's control
(that would make it a bit easy to catch!). The plan is to pick a
location on the stage at random and then head towards it for a
while. Once the star gets there we will pick a new location and move
towards that, and keep this up until the Bird catches it.</p><p>To start with we want to appear on the stage a little bit away from
the Bird. The star costume is also kind of big so I'll set the size at
the same time. This should all happen when the green flag is clicked.</p><div class="patch-container" data-code-as-of-commit='import pytch


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)


class Star(pytch.Sprite):
    Costumes = ["star.png"]

    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
' data-slug="add-star-move-script-part-1"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>33</pre></td><td class="linenum"><pre>33</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>34</pre></td><td class="linenum"><pre>34</pre></td><td><pre>class Star(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>35</pre></td><td class="linenum"><pre>35</pre></td><td><pre>    Costumes = ["star.png"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text="
    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
"><tr><td class="linenum"></td><td class="linenum"><pre>36</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>37</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>38</pre></td><td><pre>    def play(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>39</pre></td><td><pre>        self.go_to_xy(-100, 100)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>40</pre></td><td><pre>        self.set_size(0.4)</pre></td></tr></tbody></table></div></div><p>I can have the star keep moving as long as the game is running, just
moving from place to place. When it gets caught we can hide it for a
while and then have it reappear at whatever position it's randomly
moved to so that the bird can continue to chase it.</p><p>To make some code that repeats I can use the Python <code>while</code> command. A
<code>while</code> loop has something it checks each time, and it will repeat its
code as long as that thing is true. Normally you put something that
could be different each time it's checked (for example, something that
checks the <code>x</code> position of a Sprite) but if you want the code to
repeat forever you can just write <code>True</code> there and it will always be
true!</p><p>Notice the <code>:</code> at the end of the line. Like classes and script
definitions, this loop will have more commands inside it (which will
be the stuff to be repeated over and over).  For now, we'll just make
sure the Star is visible, because we know that our program soon will
hide the Star when it gets caught, and we want to make sure it's
visible at the start of each movement across the stage.</p><div class="patch-container" data-code-as-of-commit='import pytch


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)


class Star(pytch.Sprite):
    Costumes = ["star.png"]

    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
        while True:
            self.show()
' data-slug="add-infinite-loop"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>38</pre></td><td class="linenum"><pre>38</pre></td><td><pre>    def play(self):</pre></td></tr><tr><td class="linenum"><pre>39</pre></td><td class="linenum"><pre>39</pre></td><td><pre>        self.go_to_xy(-100, 100)</pre></td></tr><tr><td class="linenum"><pre>40</pre></td><td class="linenum"><pre>40</pre></td><td><pre>        self.set_size(0.4)</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        while True:
            self.show()
"><tr><td class="linenum"></td><td class="linenum"><pre>41</pre></td><td><pre>        while True:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>42</pre></td><td><pre>            self.show()</pre></td></tr></tbody></table></div></div><p>Now I want to pick a random location on the stage. Python has a way to
make random numbers, so if I just pick two <code>x</code> and <code>y</code> numbers they
can be the destination for the star to move towards. When the star
gets there the loop will repeat and a new destination will be chosen.</p><p>The <code>random.randint()</code> function in Python selects a random number
between two points (so <code>random.randint(10, 20)</code> will pick a number
between 10 and 20). The Pytch Stage has x coordinates from -240 to
+240, so we'll choose a random number in that range for <code>x</code>.  This
will allow the Star to stick out beyond the edge of the stage
slightly, but that's OK.  And we'll do a similar thing for <code>y</code>, which
we'll choose between -180 and +180.</p><p>Using this I set up two variables inside the loop:</p><div class="patch-container" data-code-as-of-commit='import pytch


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)


class Star(pytch.Sprite):
    Costumes = ["star.png"]

    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
        while True:
            self.show()
            destination_x = random.randint(-240, 240)
            destination_y = random.randint(-180, 180)
' data-slug="select-star-destinations"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>40</pre></td><td class="linenum"><pre>40</pre></td><td><pre>        self.set_size(0.4)</pre></td></tr><tr><td class="linenum"><pre>41</pre></td><td class="linenum"><pre>41</pre></td><td><pre>        while True:</pre></td></tr><tr><td class="linenum"><pre>42</pre></td><td class="linenum"><pre>42</pre></td><td><pre>            self.show()</pre></td></tr></tbody><tbody class="diff-add" data-added-text="            destination_x = random.randint(-240, 240)
            destination_y = random.randint(-180, 180)
"><tr><td class="linenum"></td><td class="linenum"><pre>43</pre></td><td><pre>            destination_x = random.randint(-240, 240)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>44</pre></td><td><pre>            destination_y = random.randint(-180, 180)</pre></td></tr></tbody></table></div></div><p>Next I want to move the star to that point. But I don't want to just
have it hop there, because that will mean the star jumping around the
stage as fast as possible. It will just flicker around and the Bird
won't have any chance to catch it. Instead I want it to move smoothly
to the point over a few seconds.</p><p>Scratch has a handy block called "glide to" which does this. Pytch has
the same thing, in a method called <code>glide_to_xy()</code>.  We'll use this,
asking Pytch to make the Star take 2 seconds to glide to its
destination.</p><div class="patch-container" data-code-as-of-commit='import pytch


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)


class Star(pytch.Sprite):
    Costumes = ["star.png"]

    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
        while True:
            self.show()
            destination_x = random.randint(-240, 240)
            destination_y = random.randint(-180, 180)
            self.glide_to_xy(destination_x, destination_y, 2)
' data-slug="call-glide-to"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>42</pre></td><td class="linenum"><pre>42</pre></td><td><pre>            self.show()</pre></td></tr><tr><td class="linenum"><pre>43</pre></td><td class="linenum"><pre>43</pre></td><td><pre>            destination_x = random.randint(-240, 240)</pre></td></tr><tr><td class="linenum"><pre>44</pre></td><td class="linenum"><pre>44</pre></td><td><pre>            destination_y = random.randint(-180, 180)</pre></td></tr></tbody><tbody class="diff-add" data-added-text="            self.glide_to_xy(destination_x, destination_y, 2)
"><tr><td class="linenum"></td><td class="linenum"><pre>45</pre></td><td><pre>            self.glide_to_xy(destination_x, destination_y, 2)</pre></td></tr></tbody></table></div></div><p>There is one bit of housekeeping to attend to. The <code>random</code> functions
are not actually built-in to Python, like Pytch they are a kind of
add-on (called a "library"). We need to add a line to the top of the
project to get them included:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)


class Star(pytch.Sprite):
    Costumes = ["star.png"]

    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
        while True:
            self.show()
            destination_x = random.randint(-240, 240)
            destination_y = random.randint(-180, 180)
            self.glide_to_xy(destination_x, destination_y, 2)
' data-slug="import-random"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>1</pre></td><td class="linenum"><pre>1</pre></td><td><pre>import pytch</pre></td></tr></tbody><tbody class="diff-add" data-added-text="import random
"><tr><td class="linenum"></td><td class="linenum"><pre>2</pre></td><td><pre>import random</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>2</pre></td><td class="linenum"><pre>3</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>4</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>5</pre></td><td><pre>class Sky(pytch.Stage):</pre></td></tr></tbody></table></div></div><p>(This is very similar to how you "add an extension" in Scratch to make
more blocks available.)</p><p>If you try the project out now you'll see it's almost complete! The
Star glides around and the Bird can chase it. The only thing we need
to do is have something happen when the bird catches the Star!</p></div><div class="chapter-content"><h2>Catching the Star</h2><p>Pytch has a method called <code>touching</code> that checkes to see if two
Sprites are touching each other.</p><p>We could check this every time the Bird moves to see if it has caught
up with the Star. But we would also have to check it every time the
Star moved, in case the bird was standing still and the Star ran into
it!</p><p>Instead I will write a Script that does nothing except continuously
check to see if the Star has collided with the bird. That way no
matter which Sprite was moving we will detect it in one bit of
code. I'll use the same idea that the <code>play</code> method had to keep
checking.</p><p>This script should start up as soon as the game begins, so I give it
the same "green flag" hat as the <code>start</code> and <code>play</code> methods.</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)

    @pytch.when_green_flag_clicked
    def check_catch(self):
        while True:
            if self.touching(Star):
                self.say_for_seconds("Got you!", 2)


class Star(pytch.Sprite):
    Costumes = ["star.png"]

    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
        while True:
            self.show()
            destination_x = random.randint(-240, 240)
            destination_y = random.randint(-180, 180)
            self.glide_to_xy(destination_x, destination_y, 2)
' data-slug="bird-checks-for-catching-says-gotcha"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>31</pre></td><td class="linenum"><pre>31</pre></td><td><pre>    def move_down(self):</pre></td></tr><tr><td class="linenum"><pre>32</pre></td><td class="linenum"><pre>32</pre></td><td><pre>        self.change_y(-self.speed)</pre></td></tr><tr><td class="linenum"><pre>33</pre></td><td class="linenum"><pre>33</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text='    @pytch.when_green_flag_clicked
    def check_catch(self):
        while True:
            if self.touching(Star):
                self.say_for_seconds("Got you!", 2)

'><tr><td class="linenum"></td><td class="linenum"><pre>34</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>35</pre></td><td><pre>    def check_catch(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>36</pre></td><td><pre>        while True:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>37</pre></td><td><pre>            if self.touching(Star):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>38</pre></td><td><pre>                self.say_for_seconds("Got you!", 2)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>39</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>34</pre></td><td class="linenum"><pre>40</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>35</pre></td><td class="linenum"><pre>41</pre></td><td><pre>class Star(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>36</pre></td><td class="linenum"><pre>42</pre></td><td><pre>    Costumes = ["star.png"]</pre></td></tr></tbody></table></div></div><p>If you try the project now you'll see that this is <em>almost</em> perfect,
but there are two problems:</p><h3>Hiding the star when it's caught</h3><p>After the bird catches the star we want the star to vanish for a
time. Otherwise after the bird says "Got you" the star will be very
nearby (maybe even still touching the bird!) and it's too easy to
catch it.</p><p>One way we could do this is to have the Star also constantly check to
see whether it's been caught, and hide itself as soon as it
is. Because the loop in <code>play</code> starts with <code>show</code> the Star will
reappear when it has finished gliding to its destination, and the game
can continue.</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)

    @pytch.when_green_flag_clicked
    def check_catch(self):
        while True:
            if self.touching(Star):
                self.say_for_seconds("Got you!", 2)


class Star(pytch.Sprite):
    Costumes = ["star.png"]

    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
        while True:
            self.show()
            destination_x = random.randint(-240, 240)
            destination_y = random.randint(-180, 180)
            self.glide_to_xy(destination_x, destination_y, 2)

    @pytch.when_green_flag_clicked
    def check_caught(self):
        while True:
            if self.touching(Bird):
                self.hide()
' data-slug="star-checks-for-catching"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>50</pre></td><td class="linenum"><pre>50</pre></td><td><pre>            destination_x = random.randint(-240, 240)</pre></td></tr><tr><td class="linenum"><pre>51</pre></td><td class="linenum"><pre>51</pre></td><td><pre>            destination_y = random.randint(-180, 180)</pre></td></tr><tr><td class="linenum"><pre>52</pre></td><td class="linenum"><pre>52</pre></td><td><pre>            self.glide_to_xy(destination_x, destination_y, 2)</pre></td></tr></tbody><tbody class="diff-add" data-added-text="
    @pytch.when_green_flag_clicked
    def check_caught(self):
        while True:
            if self.touching(Bird):
                self.hide()
"><tr><td class="linenum"></td><td class="linenum"><pre>53</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>54</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>55</pre></td><td><pre>    def check_caught(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>56</pre></td><td><pre>        while True:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>57</pre></td><td><pre>            if self.touching(Bird):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>58</pre></td><td><pre>                self.hide()</pre></td></tr></tbody></table></div></div><h3>Fixing a start-up bug</h3><p>You might notice that the bird catches the star as soon as we start
the program.  This is because the star hasn't had a chance to move
before the bird checks whether it's caught the star.  We'll work round
this problem by making the Bird wait for a tiny delay before starting
checking for touching the star:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)

    @pytch.when_green_flag_clicked
    def check_catch(self):
        pytch.wait_seconds(0.1)
        while True:
            if self.touching(Star):
                self.say_for_seconds("Got you!", 2)


class Star(pytch.Sprite):
    Costumes = ["star.png"]

    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
        while True:
            self.show()
            destination_x = random.randint(-240, 240)
            destination_y = random.randint(-180, 180)
            self.glide_to_xy(destination_x, destination_y, 2)

    @pytch.when_green_flag_clicked
    def check_caught(self):
        while True:
            if self.touching(Bird):
                self.hide()
' data-slug="delay-detect-star-loop"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>33</pre></td><td class="linenum"><pre>33</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>34</pre></td><td class="linenum"><pre>34</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"><pre>35</pre></td><td class="linenum"><pre>35</pre></td><td><pre>    def check_catch(self):</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        pytch.wait_seconds(0.1)
"><tr><td class="linenum"></td><td class="linenum"><pre>36</pre></td><td><pre>        pytch.wait_seconds(0.1)</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>36</pre></td><td class="linenum"><pre>37</pre></td><td><pre>        while True:</pre></td></tr><tr><td class="linenum"><pre>37</pre></td><td class="linenum"><pre>38</pre></td><td><pre>            if self.touching(Star):</pre></td></tr><tr><td class="linenum"><pre>38</pre></td><td class="linenum"><pre>39</pre></td><td><pre>                self.say_for_seconds("Got you!", 2)</pre></td></tr></tbody></table></div></div></div><div class="chapter-content"><h2>Adding some sound</h2><p>To make the game a bit more fun we'll have the bird make an excited
noise when it catches the Star.</p><p>We can add <em>sounds</em> to a Sprite using the Sounds variable.</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    Sounds = ["honk.wav"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)

    @pytch.when_green_flag_clicked
    def check_catch(self):
        pytch.wait_seconds(0.1)
        while True:
            if self.touching(Star):
                self.say_for_seconds("Got you!", 2)


class Star(pytch.Sprite):
    Costumes = ["star.png"]

    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
        while True:
            self.show()
            destination_x = random.randint(-240, 240)
            destination_y = random.randint(-180, 180)
            self.glide_to_xy(destination_x, destination_y, 2)

    @pytch.when_green_flag_clicked
    def check_caught(self):
        while True:
            if self.touching(Bird):
                self.hide()
' data-slug="add-bird-sounds"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>8</pre></td><td class="linenum"><pre>8</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>9</pre></td><td><pre>class Bird(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>10</pre></td><td class="linenum"><pre>10</pre></td><td><pre>    Costumes = ["bird.png"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text='    Sounds = ["honk.wav"]
'><tr><td class="linenum"></td><td class="linenum"><pre>11</pre></td><td><pre>    Sounds = ["honk.wav"]</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>12</pre></td><td><pre>    speed = 3</pre></td></tr><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>13</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>14</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr></tbody></table></div></div><p>And we can add a line to the bird's <code>check_catch</code> method that plays
this sound when the star is caught. The script will go on to the next
command immediately, but because of the <code>say</code> command it will wait for
about the right amount of time for the sound to finish.</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    Sounds = ["honk.wav"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)

    @pytch.when_green_flag_clicked
    def check_catch(self):
        pytch.wait_seconds(0.1)
        while True:
            if self.touching(Star):
                self.start_sound("honk")
                self.say_for_seconds("Got you!", 2)


class Star(pytch.Sprite):
    Costumes = ["star.png"]

    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
        while True:
            self.show()
            destination_x = random.randint(-240, 240)
            destination_y = random.randint(-180, 180)
            self.glide_to_xy(destination_x, destination_y, 2)

    @pytch.when_green_flag_clicked
    def check_caught(self):
        while True:
            if self.touching(Bird):
                self.hide()
' data-slug="play-caught-sound"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>37</pre></td><td class="linenum"><pre>37</pre></td><td><pre>        pytch.wait_seconds(0.1)</pre></td></tr><tr><td class="linenum"><pre>38</pre></td><td class="linenum"><pre>38</pre></td><td><pre>        while True:</pre></td></tr><tr><td class="linenum"><pre>39</pre></td><td class="linenum"><pre>39</pre></td><td><pre>            if self.touching(Star):</pre></td></tr></tbody><tbody class="diff-add" data-added-text='                self.start_sound("honk")
'><tr><td class="linenum"></td><td class="linenum"><pre>40</pre></td><td><pre>                self.start_sound("honk")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>40</pre></td><td class="linenum"><pre>41</pre></td><td><pre>                self.say_for_seconds("Got you!", 2)</pre></td></tr><tr><td class="linenum"><pre>41</pre></td><td class="linenum"><pre>42</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>42</pre></td><td class="linenum"><pre>43</pre></td><td><pre></pre></td></tr></tbody></table></div></div></div><div class="chapter-content"><h2>Add instructions</h2><p>To tell the player how to use our game, we'll add some instructions at
the top of our code.  We'll do this using a Python <em>comment</em>, which is
a part of your program meant just for human readers — Python ignores
it.  In Python, a line starting with the <code>#</code> character is a comment.
We'll add a short explanation of the aim of the game, and how you move
the bird, using a comment line:</p><div class="patch-container" data-code-as-of-commit='# Move the bird using the arrow keys and try to catch the star!

import pytch
import random


class Sky(pytch.Stage):
    Backdrops = ["clouds.jpg"]


class Bird(pytch.Sprite):
    Costumes = ["bird.png"]
    Sounds = ["honk.wav"]
    speed = 3

    @pytch.when_green_flag_clicked
    def start(self):
        self.go_to_xy(0, 0)
        self.set_size(0.3)

    @pytch.when_key_pressed("ArrowRight")
    def move_right(self):
        self.change_x(self.speed)

    @pytch.when_key_pressed("ArrowLeft")
    def move_left(self):
        self.change_x(-self.speed)

    @pytch.when_key_pressed("ArrowUp")
    def move_up(self):
        self.change_y(self.speed)

    @pytch.when_key_pressed("ArrowDown")
    def move_down(self):
        self.change_y(-self.speed)

    @pytch.when_green_flag_clicked
    def check_catch(self):
        pytch.wait_seconds(0.1)
        while True:
            if self.touching(Star):
                self.start_sound("honk")
                self.say_for_seconds("Got you!", 2)


class Star(pytch.Sprite):
    Costumes = ["star.png"]

    @pytch.when_green_flag_clicked
    def play(self):
        self.go_to_xy(-100, 100)
        self.set_size(0.4)
        while True:
            self.show()
            destination_x = random.randint(-240, 240)
            destination_y = random.randint(-180, 180)
            self.glide_to_xy(destination_x, destination_y, 2)

    @pytch.when_green_flag_clicked
    def check_caught(self):
        while True:
            if self.touching(Bird):
                self.hide()
' data-slug="add-player-instructions"><div class="patch"><table><tbody class="diff-add" data-added-text="# Move the bird using the arrow keys and try to catch the star!

"><tr><td class="linenum"></td><td class="linenum"><pre>1</pre></td><td><pre># Move the bird using the arrow keys and try to catch the star!</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>2</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>1</pre></td><td class="linenum"><pre>3</pre></td><td><pre>import pytch</pre></td></tr><tr><td class="linenum"><pre>2</pre></td><td class="linenum"><pre>4</pre></td><td><pre>import random</pre></td></tr><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>5</pre></td><td><pre></pre></td></tr></tbody></table></div></div></div><div class="chapter-content"><h2>Challenges</h2><ol>
<li>
<p>You could add a score to the game so that the bird says things like
   "Got you 5 times". Create a variable in the Bird sprite, and add
   one to it every time the star is caught. You can use a variable
   called "score" in the <code>say()</code> command like this: <code>self.say("Score
   is " + str(self.score))</code>. The <code>str()</code> command converts a number to
   a string so that it can be joined with another string using <code>+</code>.</p>
</li>
<li>
<p>You could add a second Sprite to chase, which moves faster.</p>
</li>
<li>
<p>You could make the game harder by having the Bird slow down a
   little after it catches 5 stars (by changing the <code>speed</code>
   variable). After a while you might like to have the Bird speed up
   again (maybe after some time passes, or after it catches the star
   again).</p>
</li>
</ol></div><div class="chapter-content"><h2>Credits</h2><p>We have used various freely-available resources to help make this
project:</p><div class="asset-credits"><p class="credit-intro">For <code class="asset-filename">screenshot.png</code> (used in the tutorial text/summary):</p><div class="credits"><p>The screenshot was created by the Pytch team, using the images from
the project.  See the credits for those individual images for more
information.</p></div><p class="credit-intro">For <code class="asset-filename">clouds.jpg</code> (used in the project):</p><div class="credits"><p>The image <a href="https://www.mepixels.com/photo/cartoon-clouds-2793"><em>Cartoon
clouds</em></a> has been
placed into the public domain by Me Pixels user <em>Emma</em>.  We have
cropped and resized it to the Pytch stage dimensions.</p></div><p class="credit-intro">For <code class="asset-filename">bird.png</code> (used in the project):</p><div class="credits"><p>The flying bird image is taken from <a href="https://openclipart.org/detail/298858/flying-birds"><em>Flying
birds</em></a>, which has
been placed into the public domain by OpenClipArt user <em>oksmith</em>.  We
are using the <a href="https://openclipart.org/detail/299098/flying-bird-animated"><em>Flying Bird
Animated</em></a>
remix by OpenClipArt user <em>JayNick</em>.</p></div><p class="credit-intro">For <code class="asset-filename">star.png</code> (used in the project):</p><div class="credits"><p>The star character image is based on
<a href="https://en.wikibooks.org/wiki/File:StarFirst.png">"StarFirst.png"</a>,
which is copyright Wikimedia user SuSchu.  We use it under the
<a href="https://creativecommons.org/licenses/by-sa/3.0/">Creative Commons "CC BY-SA 3.0"
licence</a>.  We have
cropped the original image.</p></div><p class="credit-intro">For <code class="asset-filename">honk.wav</code> (used in the project):</p><div class="credits"><p>The honk sound is based on
<a href="https://freesound.org/people/JackWilDesign/sounds/607902/">"SquawkSFX"</a>,
by FreeSound user JackWilDesign, who has placed it into the public
domain.  We have slowed down and amplified the original.</p></div></div></div></div>